package puzzle.slilin2.view.swing;

import java.awt.Color;
import java.awt.Graphics2D;

import puzzle.common.point.Pnt;
import puzzle.common.view.swing.AbstractPuzzleViewPanel;
import puzzle.slilin2.data.LineCellState;
import puzzle.slilin2.data.NumberCellState;
import puzzle.slilin2.data.SlilinPntHelper;
import puzzle.slilin2.data.SlilinBoard;

@SuppressWarnings("serial")
public class SlilinViewPanel extends AbstractPuzzleViewPanel<SlilinBoard> {

	@Override
	protected void paintBoard(Graphics2D g2, SlilinBoard b) {
		g2.setColor(Color.BLACK);

		for (int y = 0; y < b.getLogicalHeight(); y++) {
			for (int x = 0; x < b.getLogicalWidth(); x++) {
				Pnt p = SlilinPntHelper.getPhysicalPoint(new Pnt(x, y));
				int n = b.getNumber(p);
				if (n != NumberCellState.EMPTY) {
					g2.setColor(b.isDirty(p.x, p.y) ? Color.BLUE : Color.BLACK);
					String s = String.valueOf(n);
					drawStringInCell(g2, x, y, s);
				}
			}
		}

		g2.setStroke(m_thinStroke);
		for (int y = 0; y <= b.getLogicalHeight(); y++) {
			for (int x = 0; x <= b.getLogicalWidth(); x++) {
				Pnt p = SlilinPntHelper.getPhysicalPoint(new Pnt(x - 1, y - 1));
				g2.setColor(b.isDirty(p.x, p.y) ? Color.BLUE : Color.BLACK);
				int x0 = x * m_cellSize - m_dotSize;
				int y0 = y * m_cellSize - m_dotSize;
				int d = m_dotSize * 2 + 1;
				g2.fillOval(x0, y0, d, d);
			}
		}

		for (int y = 0; y < b.getLogicalHeight(); y++) {
			for (int x = 0; x <= b.getLogicalWidth(); x++) {
				Pnt p = new Pnt(x * 2 + 1, y * 2 + 2);
				LineCellState s = b.getLineCellState(p);
				g2.setColor(s.isAssumed() ? Color.RED : b.isDirty(x, y) ? Color.BLUE : Color.BLACK);
				if (s.exists()) {
					int x0 = x * m_cellSize;
					int y0 = y * m_cellSize;
					int y1 = y0 + m_cellSize;
					g2.setStroke(m_lineStroke);
					g2.drawLine(x0, y0, x0, y1);
				} else if (s.isForbidden()) {
					int x0 = x * m_cellSize;
					int y0 = y * m_cellSize + m_cellSize / 2;
					drawCross(g2, x0, y0, m_dotSize);
				}
			}
		}

		for (int y = 0; y <= b.getLogicalHeight(); y++) {
			for (int x = 0; x < b.getLogicalWidth(); x++) {
				Pnt p = new Pnt(x * 2 + 2, y * 2 + 1);
				LineCellState s = b.getLineCellState(p);
				g2.setColor(s.isAssumed() ? Color.RED : b.isDirty(x, y) ? Color.BLUE : Color.BLACK);
				if (s.exists()) {
					int x0 = x * m_cellSize;
					int y0 = y * m_cellSize;
					int x1 = x0 + m_cellSize;
					g2.setStroke(m_lineStroke);
					g2.drawLine(x0, y0, x1, y0);
				} else if (s.isForbidden()) {
					int x0 = x * m_cellSize + m_cellSize / 2;
					int y0 = y * m_cellSize;
					drawCross(g2, x0, y0, m_dotSize);
				}
			}
		}
	}

}
